/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.tkorg.coauthorgraph.servlet.graph;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import uit.tkorg.coauthorgraph.dto.ConferenceDTO;

/**
 *
 * @author Administrator
 */
public class LoadProgramCommitteeFromFileServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        ArrayList confDTOList = (ArrayList) session.getAttribute("confDTOList");
        if (confDTOList == null || confDTOList.size() == 0) {
            confDTOList = new ArrayList();
        }

        File fileUpLoad = null;
        File destinationDir = null;
        ArrayList programCommitteeAuthorList = new ArrayList();
        try {
            // Check that we have a file upload request
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            List /* FileItem */ items = upload.parseRequest(request);

            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    String fieldName = item.getFieldName();
                    String fileName = item.getName();
                    String contentType = item.getContentType();
                    boolean isInMemory = item.isInMemory();
                    long sizeInBytes = item.getSize();
                    byte[] content = item.get();
                    String realPath = getServletContext().getRealPath("");
                    boolean existed = false;
                    if (confDTOList != null && confDTOList.size() > 0) {
                        for (int i=0; i< confDTOList.size(); i++) {
                            ConferenceDTO confDTO = (ConferenceDTO) confDTOList.get(i);
                            if (confDTO.getConferenceName().equalsIgnoreCase(fileName)) {
                                existed = true;
                                break;
                            }
                        }
                    }

                    if (!existed) {
                        destinationDir = new File(realPath);
                        fileUpLoad = new File(destinationDir, fileName);
                        item.write(fileUpLoad);
                        BufferedReader reader = new BufferedReader(new FileReader(fileUpLoad));
                        String programCommitteeAuthorLine;
                        while ((programCommitteeAuthorLine = reader.readLine()) != null) {
                            programCommitteeAuthorList.add(programCommitteeAuthorLine);
                        }

                        // add a new program committee to the list
                        ConferenceDTO confDTO = new ConferenceDTO();
                        confDTO.setConferenceName(fileName);
                        confDTO.setAuthorListOfProgCommittee(programCommitteeAuthorList);
                        confDTOList.add(confDTO);
                        request.setAttribute("resultMessage", "Loading Programm Committee is done");
                    }
                    else {
                        request.setAttribute("resultMessage", "The program committee existed");
                    }
                }
            }

            session.setAttribute("confDTOList", confDTOList);
            RequestDispatcher dispatcher = request.getRequestDispatcher("./jsp/ProgramCommiteeGraphBuilding.jsp");
            dispatcher.forward(request, response);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
